﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	RulesSource.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Collections.Generic;

namespace Smart.Common.Rules
{
	/// <summary>
	/// Rules source
	/// </summary>
	public abstract class RulesSource
	{
		/// <summary>
		/// Rule context
		/// </summary>
		public RulesCtx Ctx { get; set; }

		/// <summary>
		/// Rules source
		/// </summary>
		/// <param name="ctx">Context</param>
		protected RulesSource(RulesCtx ctx)
		{
			Ctx = ctx;
		}

		/// <summary>
		/// Rules filter
		/// </summary>
		public abstract RulesFilter CreateFilter();

		/// <summary>
		/// Load instance by instance id
		/// </summary>
		/// <param name="instanceId">Instance id</param>
		/// <returns>Instance of TInstanceType</returns>
		public abstract IRulesInstance LoadInstance(string instanceId);

		/// <summary>
		/// Load rules instance by rules filter
		/// </summary>
		/// <param name="filter">Filter to load rules instance</param>
		/// <returns>Rules instance</returns>
		public abstract IRulesInstance LoadInstance(RulesFilter filter);

		/// <summary>
		/// Parse rules and load to context
		/// </summary>
		public abstract void ParseRulesToCtx(object rulesSource);

		/// <summary>
		/// Parse rule
		/// </summary>
		public abstract IRule ParseRule(object ruleSource);

		/// <summary>
		/// Parse rule condition
		/// </summary>
		public abstract IRuleCondition ParseCondition(object ruleCondition);

		/// <summary>
		/// Parse rule action
		/// </summary>
		public abstract IRuleAction ParseAction(object ruleAction);

		/// <summary>
		/// Load instance variables to context
		/// </summary>
		public abstract void LoadInstanceVariablesToCtx();

		/// <summary>
		/// Load supplied variables to context
		/// </summary>
		public virtual void LoadVariablesToCtx(IDictionary<string, object> variables)
		{
			if (variables == null)
				return;

			var ctxVariables = Ctx.Variables;

			foreach (var kvp in variables)
				ctxVariables[kvp.Key] = kvp.Value;
		}
	}
}